* SETUP DATA WITH ORIGIN OF MOVERS INTO OW

version 13.1

set more off
cap clear matrix
cap graph drop _all
cap log close

quiet{
* get the data with all the indicators&dummies needed
use "/Volumes/OW_data/my_dta_v8.1.dta", clear
label var zuzugjahre "move-in year (including several moves)"
label var zuzugjahr "move-in year (last move)"
drop if year>2010
drop if zuzugjahre>2010
bys year: egen residents=count(persid)

drop if zuzugkt==6
drop if zuzugjahre < 2001

* Adjust taxable income to account for the changes in deduction in 2008
replace esteuerbst=esteuerbst+10000 if year>=2008 // to account for the increased deduction under the flat-tax regime

set scheme mygraphs
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 


* GENERATE THE TREATMENT DEFINITION BASED ON RATE DETERMINING INCOME
gen taxinc_thre2 = (esatzbestkopf>= 300000 & esatzbestkopf <.)
label var taxinc_thre2 "Taxable income threshold regressive tax"

gen ctrl_ir_55_75 = (esatzbestkopf>= 300000*0.55 & esatzbestkopf< 300000*0.75)
label var ctrl_ir_55_75 "Control group: 55-75% of income threshold"

gen ctrl_ir_60_80 = (esatzbestkopf>= 300000*0.60 & esatzbestkopf< 300000*0.80)
label var ctrl_ir_60_80 "Control group: 60-80% of income threshold"

gen ctrl_ir_60_85 = (esatzbestkopf>= 300000*0.60 & esatzbestkopf< 300000*0.85)
label var ctrl_ir_60_85 "Control group: 60-85% of income threshold"

gen ctrl_ir_60_95 = (esatzbestkopf>= 300000*0.60 & esatzbestkopf< 300000*0.95)
label var ctrl_ir_60_95 "Control group: 60-95% of income threshold"

gen ctrl_ir_45_70 = (esatzbestkopf>= 300000*0.45 & esatzbestkopf< 300000*0.70)
label var ctrl_ir_45_70 "Control group: 40-70% of income threshold"

* Generate a dummy for each bracket
gen regbr1		= (esatzbestkopf>=300001 & esatzbestkopf<=340000)
gen regbr2		= (esatzbestkopf>=340001 & esatzbestkopf<=380000)
gen regbr3		= (esatzbestkopf>=380001 & esatzbestkopf<=420000)
gen regbr4		= (esatzbestkopf>=420001 & esatzbestkopf<=550000)
gen regbr5		= (esatzbestkopf>=550001 & esatzbestkopf<=600000)
gen regbr6		= (esatzbestkopf>=600001 & esatzbestkopf<=1000000)
gen regbr7		= (esatzbestkopf>=1000001 & esatzbestkopf<. )

* DEFLATE THE INCOME VARIABLES
merge m:1 year using "$mypathRR/Datasets/CPI.dta"

drop _merge
drop if year==.

foreach var in esteuerbst vsteuerbst esatzbestkopf vsatzbestkopf steuern_eink steuern_verm z199s1 z450s1 z300s1 z470s1 inmob_inc mob_inc {
replace `var'=`var'/cpi*100.5 //deflate the series
local u : variable label `var'
local l= "`u' (real)" 
label var `var' "`l'"
}
 
* * * PREPARE SOME VARIABLES TO PROPERLY IDENTIFY INCOME AND WEALTH IN THE MOVE-IN YEAR AND STILL COUNT THE incomers CORRECTLY
sort persid year
egen freq= tag(persid zuzugjahre) //this will be used to select the incomers

foreach var in sex age married doppel snpanzki employee self_emp farmer retiree labor_ratio self_ratio cap_ratio inmob_ratio mob_ratio labor_index3 mob_index3 mob_inc labor_inc self_inc  woaufenthalt z220s1 z221s1 z222s1 z223s1 z255s1 z256s1 swiss zuzugausland pauschaliert ///
			esteuerbst vsteuerbst esatzbestkopf vsatzbestkopf steuern_eink steuern_verm z199s1 z450s1 z300s1 z470s1 inmob_inc  taxinc_dez taxinc_thre2 taxwea_thre2 taxwea_dez ctrl_ir_55_75 ctrl_ir_60_80 ctrl_ir_60_85 ctrl_ir_60_95 ctrl_ir_45_70 ///
			regbr1 regbr2 regbr3 regbr4 regbr5 regbr6 regbr7 {
  gen yrz=(year==zuzugjahre)
  replace yrz=yrz*(-1)
  gen `var'_yz=.
  bys persid (year): replace `var'_yz=`var' if yrz==-1
  bys persid _spell (yrz): replace yrz=yrz[1]
  bys persid (year): replace `var'_yz=`var'_yz[_n-1] if yrz==-1 & `var'_yz==.
  
  forvalues z=1/10 {
    local i=`z'
    bys persid (year): replace `var'_yz=`var'_yz[_n+1] if yrz==-1 & `var'_yz==. // note: repeat this step until 0 real changes made
    }
  drop yrz
}

foreach var in sex age married doppel snpanzki employee self_emp farmer retiree labor_ratio self_ratio cap_ratio inmob_ratio mob_ratio labor_index3 mob_index3 mob_inc labor_inc self_inc  woaufenthalt z220s1 z221s1 z222s1 z223s1 z255s1 z256s1 swiss zuzugausland pauschaliert ///
			esteuerbst vsteuerbst esatzbestkopf vsatzbestkopf steuern_eink steuern_verm z199s1 z450s1 z300s1 z470s1 inmob_inc  taxinc_dez taxinc_thre2 taxwea_thre2 taxwea_dez ctrl_ir_55_75 ctrl_ir_60_80 ctrl_ir_60_85 ctrl_ir_60_95 ctrl_ir_45_70 ///
			regbr1 regbr2 regbr3 regbr4 regbr5 regbr6 regbr7 {
			replace `var'=`var'_yz
}

}

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* DATA ON ORIGINS AND DESTINATIONS
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
cd "$mypathRR/Datasets/"
cap mkdir movers
cd "$mypathRR/Datasets/movers"

* ORIGIN: CANTON
keep if taxinc_thre2==1

preserve
	sort persid zuzugjahre year
	keep if freq==1
	
	drop if zuzugvonbfs>8000 // drop those who moved in from abroad

	collapse (count) Inmovers=persid (mean) avg_determining=esatzbestkopf (mean) avg_taxable=esteuerbst, by(zuzugjahre zuzugkt)
	label var Inmovers "Movers to OW"
	label var avg_determining "Average rate determining income"
	label var avg_taxable "Average taxable income"
	
	rename zuzugkt cant
	rename zuzugjahre year

	sort cant year
	xtset cant year
	tsfill, full
	replace Inmovers = 0 if Inmovers == . 
	bys cant (year): gen Inmovers_cum = sum(Inmovers)  if year >2005
	replace Inmovers_cum = 0 if Inmovers_cum ==. 

	save "movers_cant_origin.dta", replace
	
	collapse (sum) Movers_destin = Inmovers   Movers_destin_cum = Inmovers_cum , by(year)
	gen cant = 6
	save "movers_cant_destination.dta", replace

restore


* ORIGIN: MUNICIPALITIES
preserve
	sort persid zuzugjahre year
	keep if freq==1
	 
	collapse (count) Movers_origin=persid (mean) avg_determining=esatzbestkopf avg_taxable=esteuerbst cant=zuzugkt, by(zuzugvonbfs zuzugjahre)
	label var Movers_origin "Movers to OW"
	label var avg_determining "Average rate determining income"
	label var avg_taxable "Average taxable income"
	
	rename zuzugjahre year
	rename zuzugvonbfs gemeinde
	
	sort gemeinde year
	xtset gemeinde year
	tsfill, full
	replace Movers_origin = 0 if Movers_origin == . 
	
	bys gemeinde (year): gen Movers_origin_cum = sum(Movers_origin) if year >2005
	replace Movers_origin_cum = 0 if Movers_origin_cum ==. 

	save "movers_gemeinde_origin.dta", replace
	
restore



* DESTINATION: MUNICIPALITIES
preserve
	sort persid zuzugjahre year
	keep if freq==1

	collapse (count) Movers_destin=persid, by(zuzugvonbfs ste_prigde_code zuzugjahre)
	label var Movers_destin "Inmovers in OW"
	
	rename zuzugjahre year

	rename ste_prigde_code gemeinde

	* recode the municipalities
	recode gemeinde (1=70)
	recode gemeinde (2=40)
	recode gemeinde (3=60)
	recode gemeinde (4=10)
	recode gemeinde (5=30)
	recode gemeinde (6=50)
	recode gemeinde (7=20)
	forval n=10(10)70 {
	local i=`n'/10
	recode gemeinde (`n'=`i')
	}
	replace gemeinde = gemeinde + 1400

	label define GEMEINDE 1401 `"Alpnach"', modify
	label define GEMEINDE 1402 `"Engelberg"', modify
	label define GEMEINDE 1403 `"Giswil"', modify
	label define GEMEINDE 1404 `"Kerns"', modify
	label define GEMEINDE 1405 `"Lungern"', modify
	label define GEMEINDE 1406 `"Sachseln"', modify
	label define GEMEINDE 1407 `"Sarnen"', modify
	label values gemeinde GEMEINDE

	sort gemeinde year
	save "movers_gemeinde_dest+orig.dta", replace
		
	drop if zuzugvonbfs>8000 // drop those who moved in from abroad

	collapse (sum) Movers_destin , by(gemeinde year)
	
	bys gemeinde (year): gen Movers_destin_cum = sum(Movers_destin) if year >2005
	replace Movers_destin_cum = 0 if Movers_destin_cum ==. 
	
	save "movers_gemeinde_destination.dta", replace

restore



